<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%@include file="/common/common.jsp" %>
    <link rel="stylesheet" href="${basePath}/css/layerPage.css${VERSION}"/>
</head>
<body>
<form id="form" name="form" class="form-horizontal" method="post" action="${basePath}/userMemerShip/modify">
    <div class="panel-body">
        <input type="hidden" name="id" id="id" value="${userMemership.id}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label">选择流程：</label>
            <div class="col-sm-9" style="width:200px">
                <select id="procinstKey" name="procinstKey" class="selectpicker show-tick form-control" value="${userMemership.procinstKey}">
                    <option value="JyleaveBill" >请假</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">选择节点：</label>
            <div class="col-sm-9" style="width:200px">
                <select id="groupKey" name="groupKey" class="selectpicker show-tick form-control">
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">选择部门：</label>
            <div class="col-sm-9">
                <input class="col-sm-5" type="text" name="orgName" id="orgName" readonly="true" value="${userMemership.orgName}"/>
                <input type="hidden" name="orgId" id="orgId" value="${userMemership.orgId}"/>
                <button type="button" id="chooseOrgBtn" class="btn btn-danger">选择部门</button>
            </div>
        </div>

        <div class="form-group" id="taskUser">
            <label class="col-sm-3 control-label">选择审批人：</label>
            <div class="col-sm-9" style="width:120px">
                <input type="hidden" name="alloterId" id="alloterId"/>
                <input type="hidden" name="alloterIds" id="alloterIds" value="${userMemership.userId}"/>
                <input class="col-sm-5" type="text" name="userName" id="userName" data-toggle="dropdown" readonly style="width:400px" value="${userMemership.userName}"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">是否主负责人：</label>
            <div class="col-sm-9" style="width:200px">
                <select id="isMain" name="isMain" class="selectpicker show-tick form-control">
                    <option value="1" >是</option>
                    <option value="0" >否</option>
                </select>
            </div>
        </div>

        <button id="submit" type="submit" class="btn btn-primary btn-submit">提交</button>
    </div>
</form>
<script src="${basePath}/js/common/jquery.validator.addmethod.js${VERSION}"></script>
<script type="text/javascript">
    jQuery(function ($) {

        getDropDownItemSelected("procinstKey","${userMemership.procinstKey}");
        getFlowNodesById();
        getDropDownItemSelected("isMain","${userMemership.isMain}");

    	if(parent.userOrgId){
    		$("#orgName").val(parent.userOrgName);
    		$("#orgId").val(parent.userOrgId);
    	}

        var $form = $("#form");
        var $submit = $("#submit");

        $form.validate({
            submitHandler: function (form) {
                var id=$("#id").val();

                var procinstKey=$("#procinstKey").val();
                if(procinstKey=="")
                {
                    layer.alert("请选择流程", {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                    return;
                }

                var groupKey=$("#groupKey").val();
                if(groupKey=="")
                {
                    layer.alert("请选择节点", {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                    return;
                }

                var orgId=$("#orgId").val();
                if(orgId=="")
                {
                    layer.alert("请选择部门", {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                    return;
                }

                var userId=$("#alloterIds").val();
                if(userId==""||userId.indexOf(",")>-1)
                {
                    layer.alert("请选择一个审批人", {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                    return;
                }

                var isMain=$("#isMain").val();

                $.ajax({
                    url: $form.attr("action"),
                    type: $form.attr("method"),
                    data: {
                        id:id,
                        procinstKey:procinstKey,
                        groupKey:groupKey,
                        userId:userId,
                        orgId:orgId,
                        isMain:isMain
                    },
                    dataType: "json",
                    cache: false,
                    beforeSend: function () {
                        $submit.prop("disabled", true);
                    },
                    success: function (data) {
                        if (data.status == '0') {
                            $submit.prop("disabled", false);
                            layer.confirm('保存成功！是否关闭窗口？', {icon: 1}, function (index) {
                                parent.$("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
                                parent.layer.close(parent.pageii);
                                return false
                            });
                        } else {
                            $submit.prop("disabled", false);
                            layer.alert(data.message, {
                                icon: 0,
                                skin: 'layui-layer-lan',
                                closeBtn: 0
                            });
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        $submit.prop("disabled", false);
                        layer.alert("系统未知错误", {
                            icon: 0,
                            skin: 'layui-layer-lan',
                            closeBtn: 0
                        });
                    }
                });
            }
        });

        $('#chooseOrgBtn').click("click", function () {
            openOrg();
        });

        $('#procinstKey').on('change',function(){
            getFlowNodesById();
        });

        $("#userName").click("click", function () {
            openOrgPeople();
        });

        //选择人员
        function openOrgPeople() {
            var pageii = layer.open({
                title: false,
                type: 2,
                area: ["760px", "550px"],
                content: ['${basePath}/task/anon/toOrgTree']
            });
        }

        //加载流程节点
        function getFlowNodesById() {
            var procinstKey=document.getElementById("procinstKey").value;
            var groupKey='${userMemership.groupKey}';
            $.ajax({
                url: '${basePath}/userMemerShip/getFlowNodesById',
                dataType: 'json',
                type: 'post',
                data:{
                    "procinstKey":procinstKey
                },
                success: function (data) {
                    if(data.status==0)
                    {
                        $("#groupKey").empty();
                        var i;
                        for(i=0;i<data.data.length;i++)
                        {
                            if(groupKey==data.data[i].itemValue)
                            {
                                $("#groupKey").append("<option value='"+data.data[i].itemValue+"' selected>"+data.data[i].itemName+"</option>");
                            }
                            else
                            {
                                $("#groupKey").append("<option value='"+data.data[i].itemValue+"'>"+data.data[i].itemName+"</option>");
                            }
                        }
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    $submit.prop("disabled", false);
                    layer.alert("系统未知错误", {
                        icon: 0,
                        skin: 'layui-layer-lan',
                        closeBtn: 0
                    });
                }
            });
        }

        //加载是否主要审批人选项
        function getDropDownItemSelected(docId,value)
        {
            var opts = document.getElementById(docId);
            for(var i=0;i<opts.options.length;i++){
                if(value==opts.options[i].value){
                    opts.options[i].selected = "selected";
                    break;
                }
            }
        }


        //选择部门
        function openOrg() {
            var pageii = layer.open({
                title: false,
                type: 2,
                area: ["300px", "400px"],
                content: ['${basePath}/org/toOrgTree'],
                btn: ["确认", "关闭"],
                yes: function (index, layero) {
                    var orgName = $(layero).find("iframe")[0].contentWindow.orgName;
                    var orgId = $(layero).find("iframe")[0].contentWindow.orgId;
                    layer.close(index);
                    $("#orgName").val(orgName);
                    $("#orgId").val(orgId);
                }, cancel: function () {
                    return true;
                }
            });
        }
    });
</script>
</body>
</html>
